package com.jxw.studyrecordprovider.util;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import com.jxw.engine.platsign.MainActivity;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/* loaded from: classes.dex */
public class StudyRecorderUtil {
    public static final String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String IS_UPLOAD = "is_upload";
    public static final int MIDDLE = 2;
    public static final String MODUL_ACTIVITY_DURATION = "modul_activity_duration";
    public static final String MODUL_ENTER_TIME = "modul_enter_time";
    public static final String MODUL_EXIT_TIME = "modul_exit_time";
    public static final String MODUL_NAME = "modul_name";
    public static final String MODUL_TYPE = "modul_type";
    public static final int PRIMARY = 1;
    public static final Uri STUDY_RECORDR_URI = Uri.parse("content://com.jxw.studyrecord.provider/studyrecorder");

    public static Uri insertStudyRecord(Context context, String str, long j, long j2, int i) {
        if (TextUtils.isEmpty(str) || j <= 0 || j2 <= 0 || (i != 1 && i != 2)) {
            throw new IllegalArgumentException("Bad args: modulName: " + str + " enterTime: " + j + " exitTime: " + j2 + " modulType: " + i);
        }
        String covertTime = covertTime(j, DEFAULT_FORMAT);
        String covertTime2 = covertTime(j2, DEFAULT_FORMAT);
        int subTimeStamp2Minute = subTimeStamp2Minute(string2Timestamp(covertTime, DEFAULT_FORMAT), string2Timestamp(covertTime2, DEFAULT_FORMAT));
        Log.e("zzj", "modulName = " + str + " enterT = " + covertTime + " exitT = " + covertTime2 + "duration = " + subTimeStamp2Minute);
        ContentResolver contentResolver = context.getContentResolver();
        ContentValues contentValues = new ContentValues();
        contentValues.put("modul_name", str);
        contentValues.put("modul_enter_time", covertTime);
        contentValues.put("modul_exit_time", covertTime2);
        contentValues.put("modul_activity_duration", Integer.valueOf(subTimeStamp2Minute));
        contentValues.put("modul_type", Integer.valueOf(i));
        Uri insert = contentResolver.insert(STUDY_RECORDR_URI, contentValues);
        JSONArray jSONArray = new JSONArray();
        JSONObject jSONObject = new JSONObject();
        try {
            jSONObject.accumulate("startTime", covertTime);
            jSONObject.accumulate("endTime", covertTime2);
            jSONObject.accumulate("apkName", str);
            jSONArray.put(jSONObject);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (jSONArray.length() > 0) {
            uploadRecorder(insert, context, jSONArray);
        }
        return insert;
    }

    public static List<StudyRecord> queryAllStudyRecord(Context context) {
        ArrayList arrayList = new ArrayList();
        JSONArray jSONArray = new JSONArray();
        Cursor query = context.getContentResolver().query(STUDY_RECORDR_URI, null, null, null, " _id DESC");
        if (query != null && query.getCount() > 0) {
            while (query.moveToNext()) {
                StudyRecord studyRecord = new StudyRecord();
                studyRecord.name = query.getString(query.getColumnIndex("modul_name"));
                studyRecord.enterTime = query.getString(query.getColumnIndex("modul_enter_time"));
                studyRecord.exitTime = query.getString(query.getColumnIndex("modul_exit_time"));
                studyRecord.duration = query.getString(query.getColumnIndex("modul_activity_duration"));
                studyRecord.type = query.getInt(query.getColumnIndex("modul_type"));
                if (query.getInt(query.getColumnIndex("is_upload")) != 1) {
                    JSONObject jSONObject = new JSONObject();
                    try {
                        jSONObject.accumulate("startTime", studyRecord.enterTime);
                        jSONObject.accumulate("endTime", studyRecord.exitTime);
                        jSONObject.accumulate("apkName", studyRecord.name);
                        jSONArray.put(jSONObject);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                arrayList.add(studyRecord);
            }
        }
        if (jSONArray.length() > 0) {
            uploadRecorder(STUDY_RECORDR_URI, context, jSONArray);
        }
        return arrayList;
    }

    private static String covertTime(long j, String str) {
        return new SimpleDateFormat(DEFAULT_FORMAT, Locale.getDefault()).format(new Date(j));
    }

    private static Timestamp string2Timestamp(String str, String str2) {
        Date date;
        try {
            date = new SimpleDateFormat(str2, Locale.getDefault()).parse(str.replace("Z", " "));
        } catch (ParseException e) {
            e.printStackTrace();
            date = null;
        }
        if (date != null) {
            return Timestamp.valueOf(new SimpleDateFormat(str2, Locale.getDefault()).format(date));
        }
        return null;
    }

    private static int subTimeStamp2Minute(Timestamp timestamp, Timestamp timestamp2) {
        return ((int) (timestamp2.getTime() - timestamp.getTime())) / 60000;
    }

    /* loaded from: classes.dex */
    public static class StudyRecord {
        public String duration;
        public String enterTime;
        public String exitTime;
        public String name;
        public int type;

        public String toString() {
            return "StudyRecord [name=" + this.name + ", enterTime=" + this.enterTime + ", exitTime=" + this.exitTime + ", duration=" + this.duration + ", type=" + this.type + "]";
        }
    }

    public static void uploadRecorder(final Uri uri, final Context context, JSONArray jSONArray) {
        OkHttpClient okHttpClient = new OkHttpClient();
        String string = context.getSharedPreferences(MainActivity.PREFERENCE, 0).getString("series", "unkown");
        Log.e("zzj", "JSONArray=" + jSONArray + ",series=" + string);
        okHttpClient.newCall(new Request.Builder().url("http://www.jiumentongbu.com/wechat/device/submitUsed").post(new FormBody.Builder().add("series", string).add("list", jSONArray.toString()).build()).build()).enqueue(new Callback() { // from class: com.jxw.studyrecordprovider.util.StudyRecorderUtil.1
            @Override // okhttp3.Callback
            public void onResponse(Call call, Response response) throws IOException {
                String string2 = response.body().string();
                Log.e("zzj", "onResponse=" + string2);
                try {
                    if (new JSONObject(string2).optBoolean("success")) {
                        ContentValues contentValues = new ContentValues();
                        contentValues.put("is_upload", (Integer) 1);
                        context.getContentResolver().update(uri, contentValues, null, null);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override // okhttp3.Callback
            public void onFailure(Call call, IOException iOException) {
                iOException.printStackTrace();
            }
        });
    }
}
